package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author xjs
 * @date 2022/9/5 16:09
 * description
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameService apUserRealnameService;

    @Override
    public ResponseResult authList(AuthDto authDto) {
        //1. 检查分页参数
        authDto.checkParam();

        //2.构建分页参数

        IPage<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());

        //3.判断分页条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();

        if (null != authDto.getStatus()){
            queryWrapper.eq(ApUserRealname::getStatus, authDto.getStatus());
        }

        //4.查询符合条件的数据
        page(page, queryWrapper);

        // 封装返回结果数据
        PageResponseResult pageResponseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }

    @Override
    public void authPass(AuthDto authDto) {
        //根据id查询数据
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApUserRealname::getId, authDto.getId());

        //更新用户审核状态
        ApUserRealname userRealname = new ApUserRealname();
        userRealname.setStatus(authDto.getStatus());
        if (null != authDto.getMsg()) userRealname.setReason(authDto.getMsg());

        apUserRealnameService.update(userRealname, queryWrapper);
    }

}
